<?php
/**
 * 上传图片，并且保存到本地dw-oc
 * Author: lyun
 * Date: 2018/12/11
 * Created by Panxsoft.
 */

namespace App\Services\Utils;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

trait UploadFile
{
    /**
     * @param UploadedFile $image
     * @param $prefix
     * @param null $old_image
     * @return bool
     */
    public static function uploadPostFileToFile(UploadedFile $image, $prefix, $old_image = null)
    {
        // 上传新图
        $image_path = Storage::disk('oc')->put($prefix, $image);

        if ($image_path) {
            // 删除原图
            Storage::disk('oc')->delete($old_image);
        } else {
            $image_path = $old_image;
        }

        return $image_path;
    }
}